package com.xxmw.transaction.controller.robot;

import com.xxmw.transaction.common.entity.P;
import com.xxmw.transaction.common.entity.R;
import com.xxmw.transaction.common.excep.service.TransactionException;
import com.xxmw.transaction.common.utils.bourse.BitforexUtil;
import com.xxmw.transaction.common.utils.bourse.OkexUtil;
import com.xxmw.transaction.controller.BaseController;
import com.xxmw.transaction.model.app.BourseApi;
import com.xxmw.transaction.model.robot.RobotOrder;
import com.xxmw.transaction.service.app.BourseApiService;
import com.xxmw.transaction.service.robot.RobotOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 交易所订单管理
 * @author luowenliang
 * @since 2020/2/17 15:49
 */
@RestController
@RequestMapping("/admin/robot/order")
public class RobotOrderController extends BaseController {

    @Autowired
    private RobotOrderService robotOrderService;

    @Autowired
    private BourseApiService bourseApiService;

    /**
     * 列表获取
     * @param pageNum
     * @param pageSize
     * @param bourse
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("robot:order:list")
    public R list(@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                  @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize,
                  @RequestParam(value = "bourse", required = false)Integer bourse,
                  @RequestParam(value = "id", required = false)Long id,
                  @RequestParam(value = "status", required = false)Integer status,
                  @RequestParam(value = "symbol", required = false)String symbol,
                  @RequestParam(value = "outId", required = false)String outId) {
        P<RobotOrder> p = robotOrderService.pageList(pageNum, pageSize, id, outId, bourse, status, symbol);
        return R.ok(p);
    }


    /**
     * 订单取消
     * @param id
     * @return
     */
    @PostMapping("/cancel")
    @RequiresPermissions("robot:order:cancel")
    public R cancel (@RequestParam(value = "id") Long id) {
        RobotOrder order = robotOrderService.getById(id);
        HashMap<Integer, BourseApi> apiMap = bourseApiService.list().stream().collect(HashMap::new, (map, api) -> map.put(api.getBourse(), api), Map::putAll);
        if (null == order) {
            return R.error("该订单不存在");
        }
        if (1 != order.getStatus()) {
            return R.error("未提交，已成交和失败订单不可取消");
        }
        switch (order.getBourse()) {
            case 1:
                //TODO
                break;
            case 2:
                //TODO
                break;
            case 3:
                OkexUtil.cancelOrder(apiMap.get(3), order);
                break;
            case 4:
                if (BitforexUtil.cancelOrder(apiMap.get(4), order)) {
                    BitforexUtil.orderInfo(apiMap.get(4), order);
                }
                break;
            default:
                throw new TransactionException("未知的交易所订单");
        }
        robotOrderService.updateById(order);
        return R.ok(0,"取消请求成功，请稍后确认订单状态");
    }

}
